package com.atxwq.shop.manager.serivce.impl;

import com.atxwq.shop.manager.mapper.SysRoleMapper;
import com.atxwq.shop.manager.mapper.SysRoleMenuMapper;
import com.atxwq.shop.manager.serivce.SysMenuService;
import com.atxwq.shop.manager.serivce.SysRoleMenuService;
import com.atxwq.shop.manager.serivce.SysRoleService;
import com.atxwq.shop.model.dto.system.AssginMenuDto;
import com.atxwq.shop.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2025/5/12 19:08
 * @Created by 12336
 */

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    /**
     * 查询角色分配了的菜单
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        //查询所有菜单数据
        List<SysMenu> sysMenuList = sysMenuService.findNodes();
        //查询角色分配的菜单数据
        List<Long> roleMenuIds = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);
        //存储到Map中返回
        HashMap<String, Object> result = new HashMap<>();
        result.put("sysMenuList", sysMenuList);
        result.put("roleMenuIds", roleMenuIds);

        return result;
    }

    /**
     * 角色分配菜单保存
     * @param assignMenuDto
     */
    @Override
    public void doAssign(AssginMenuDto assignMenuDto) {
        //1.删除角色之前分配过的id
        sysRoleMenuMapper.deleteByRoleId(assignMenuDto.getRoleId());
        //2.保存分配的数据
        List<Map<String, Number>> menuInfo = assignMenuDto.getMenuIdList();
        //判断是否分配了菜单数据
        if(!menuInfo.isEmpty() && menuInfo.size() > 0){
            sysRoleMenuMapper.doAssign(assignMenuDto);
        }
    }
}
